package com.project.weimi.fragment;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.activity.MainActivity;
import com.project.weimi.adapter.CollectionGoodsAdapter;
import com.project.weimi.entity.response.CollectionShopEntity;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.widget.ToastMessage;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GoodsFragment extends BaseFragment{

	private RelativeLayout rl_none;
	private TextView tv_collection_type;
	private Button btn_go,btn_delete;
	private LinearLayout ll_listView;
	private ListView listView;
	private CheckBox checkBox_all;
	private List<CollectionShopEntity> list;
	private CollectionGoodsAdapter adapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// 解析布局文件
		View view = inflater.inflate(R.layout.fragment_mycollection_goods, null);
		
		initView(view);
		initListViewData();
		
		return view;
	}
	
	private void initView(View view){
		rl_none = (RelativeLayout)view.findViewById(R.id.rl_none);
		tv_collection_type = (TextView)view.findViewById(R.id.tv_collection_type);
		btn_go = (Button)view.findViewById(R.id.btn_go);
		btn_delete = (Button)view.findViewById(R.id.btn_delete);
		ll_listView = (LinearLayout)view.findViewById(R.id.ll_listView);
		listView = (ListView)view.findViewById(R.id.listView);
		checkBox_all = (CheckBox)view.findViewById(R.id.checkBox_all);
		
		// 监听
		SetClickListener.setClickListener(listener, btn_go,btn_delete,checkBox_all);
		
	}
	
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.btn_go:
				Intent intent = new Intent(getActivity(),MainActivity.class);
				intent.putExtra("fragId", 0);
				startActivity(intent);
				break;
			case R.id.checkBox_all:
				if(checkBox_all.isChecked()){
					for (int i = 0; i < list.size(); i++) {
						list.get(i).setCheckBox(true);
					}
				}else{
					for (int i = 0; i < list.size(); i++) {
						list.get(i).setCheckBox(false);
					}
				}
				adapter.notifyDataSetChanged();
				break;
			case R.id.btn_delete:
				int falseCount = 0;
				List<Integer> deleteChild = new ArrayList<Integer>();
				for (int i = 0; i < list.size(); i++) {
					boolean b = list.get(i).isCheckBox();
					if(b == true){
						// Log.e("TAG", " sj " + i);
						deleteChild.add(i);
					}
					else if(b == false){
						falseCount++;
					}
				}
				
				
				if(falseCount == adapter.getList().size()){
					ToastMessage.showMyToast(getActivity(), "请选择删除项");
					return;
				}
				
				
				for (int j = deleteChild.size(); j > 0 ; j--) {
					// Log.e("TAG", " sc " + (j - 1) + "    " + deleteChild.get((j - 1)));
					list.remove(list.get(deleteChild.get((j - 1))));
				}
				adapter.notifyDataSetChanged();
				
				isAllCheck();
				break;
			default:
				break;
			}
			
		}
	};
	
	private void initListViewData(){
		list = new ArrayList<CollectionShopEntity>();
		for (int i = 0; i < 4; i++) {
			list.add(new CollectionShopEntity(false, R.drawable.a, "德国EROS伊露丝高级莹润水基润滑液角色扮演2件套--" + i, "188.00"));
		}
		
		adapter = new CollectionGoodsAdapter(getActivity(),list);
		listView.setAdapter(adapter);
	}
	
	public void isAllCheck() {
		// Log.e("TAG", "选择后的检查");
		if(null == list || list.size() <= 0){
			checkBox_all.setChecked(false);
			rl_none.setVisibility(View.VISIBLE);
			ll_listView.setVisibility(View.GONE);
			return;
		}
		
		int checkCount = 0;
		for (int i = 0; i < list.size(); i++) {
			boolean b = list.get(i).isCheckBox();
			if(b == true){
				checkCount++;
			}
		}
		
		if(checkCount > 0 && checkCount == list.size()){
			checkBox_all.setChecked(true);
		} else if(checkCount > 0 && checkCount < list.size() || checkCount == 0){
			checkBox_all.setChecked(false);
		}
	}
	
	
	
}
